#ifndef __CALIBRATION_HH
#define __CALIBRATION_HH

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

#include "TSystem.h"
#include "TEnv.h"
#include "TRandom.h"
#include "Settings.hh"

using namespace std;

class Calibration {
public:
  Calibration();
  Calibration(const char*, Settings*);
  ~Calibration();
  void ReadCalibration();
  void PrintCalibration();
  void SetFile(const char* filename){
    fInputFile = filename;
  }
  const string InputFile(){
    return fInputFile;
  };
  double DgfEnergy(int dgf, int chan, unsigned short raw);
  double AdcEnergy(int adc, int chan, unsigned short raw);
  double RemoveOffset(int forward, int quadr, unsigned short raw);
  double DeltaEnergy(int adc, int chan, double rear);
  //double StripPos(int stradc, int strchan, unsigned short strraw, int rearadc, int rearchan, unsigned short rearraw);
  double StripPos(int stradc, int strchan, unsigned short strraw, double rear);
  double RearCorrection(double rear, double pos, int quadr, bool forward);
  double CDRingEnergy(int quadr, int ring, unsigned short raw);
  double CDStripEnergy(int quadr, int strip, unsigned short raw);
private:
  int fVerbose;
  string fInputFile;
  
  Settings* fSett;
  TRandom* fRand;
  
  vector<vector<double> > fDgfOffset;
  vector<vector<double> > fDgfGain;
  vector<vector<double> > fAdcOffset;
  vector<vector<double> > fAdcGain;

  double fBeamdumpOffset;
  double fBeamdumpGain;


  double fFTilt[4];
  double fBTilt[4];
  double fFTiltOffset[4];
  double fBTiltOffset[4];

  double fCDRingOffset[4][16];
  double fCDRingGain[4][16];
  double fCDStripOffset[4][16];
  double fCDStripGain[4][16];

  int fNofDgfs;
  int fNofDgfChans;
  int fNofAdcs;
  int fNofAdcChans;

  int fBeamdumpDgf;

  //ClassDef(Calibration, 1);
};

#endif
